import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text,Input } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import Hearder from './hearder'
import './card.scss'

export default class Index extends Component {
  constructor(props) {
    super(props)
    this.state = {
    	isload:1,
    	quninfo:'',
    	shopinfo:'',
    	member:'',
    	cindex:0,
    	mobile:'',
    	appmark:CommonJs.getmark(),
    }
  }
  config = {
    navigationBarTitleText:'开通会员'
  }
  open(url,type){
    CommonJs.openTo(url)
  }
  componentWillMount () {
    var _this = this
    this.getquninfo();
    this.getshopinfo();
    this.getmemberlevel();
    this.getphone();
  }
  componentDidShow () {

  }
  getInputVal(event){
  	var m=event.target;
		var obj={};
		obj[m.id]=m.value
    this.setState(obj)
  }
  getquninfo(){
  	var url='/cluster/getEntityAndLoginId';
  	var _this=this;
  	var datainfo=Taro.getStorageSync('dateinfo');
  	var date1={id:datainfo.qid}
    CommonJs.ajaxRequest(url,date1,'GET',function(res){
    	if(res.code==200){
	    	_this.setState({
		      quninfo:res.content
		    })
	    }
    	
	  })
  }
  getshopinfo(){
  	var url='/shop/papi/v1.9/shops/'+Taro.getStorageSync('shopId');
  	var _this=this;
  	CommonJs.ajaxRequest(url,'','GET',function(res){
	    _this.setState({
	      shopinfo:res.data
	    })
	  },'wfw')
  }
  getmemberlevel(){
  	var url='/crm/papi/v1.9/member-marketing/interests/'+Taro.getStorageSync('shopId');
  	//var url='/crm/papi/v1.9/member-marketing/interests/1205'
  	var _this=this;
  	CommonJs.ajaxRequest(url,'','get',function(res){
	     _this.setState({
	      member:res.data
	    })
	  },'wfw')
  }
  choose(val){
  	this.setState({
	    cindex:val
	  })
  }
  ktfun(){
  	var index=this.state.cindex;
  	var member=this.state.member
  	var arr=member[index].membershipInterests;
  	var isgoods=0;
  	var spindex=0;
  	var _this=this;
  	if(arr&&arr.length>0){
  		for(var i=0;i<arr.length;i++){
  			if(arr[i].equityType.id==2){
  				isgoods=1
  				spindex=i
  			}
  		}
  		if(isgoods==1){
  			Taro.showActionSheet({
				  itemList: ['直接开通会员', '购买商品自动开通会员'],
				  success: function (res) {
				    var tapindex=res.tapIndex;
				    if(tapindex==0){
				    	_this.cardadd()
				    }
				    else if(tapindex==1){
				    	
							var query=[];
							var carr=arr[spindex].effectiveConditions
							for(var j=0;j<carr.length;j++){
								var obj={};
								obj.name=carr[j].conditionName;
								obj.logoUrl=carr[j].logo;
								obj.amount=carr[j].number;
								obj.itemId=carr[j].conditionId;
								obj.itemSkuId=carr[j].itemSkuId;
								obj.price=carr[j].price;
								obj.ggname=carr[j].itemSkuName;
								obj.weight=0;
								obj.malltype='';
								query.push(obj)
							}
							//console.log(JSON.stringify(query))
							if(query.length>0){
	    					_this.$preload({
								  type:query,
								  malltype:''
							  })
	   					 	CommonJs.openTo('/pages/mall/order');
	   				  }
							else{
								Taro.showToast({
									title:'商家会员等级未设置商品',
									icon: 'none',
									duration: 2000
								});
							}
				    }
				  },
				  fail: function (res) {
				    console.log(res.errMsg)
				  }
				})
  		}
  		else{
  			this.cardadd()
  		}
  	}
  	else{
  		this.cardadd()
  	}
  }
  cardadd(){
  	var mobile=this.state.mobile;
  	var _this=this;
  	if(mobile){
	  	Taro.login({
			  success(res) {
			    if (res.code) {
			      _this.cardcreate(res.code)
			    } 
			    else {
			        Taro.showToast({
			          title:'网络异常',
			          icon: 'none',
			          duration: 2000
			        })
			    }
			  }
			})
	  }
  	else{
  		Taro.showToast({
        title:'请输入开通的手机号码',
        icon: 'none',
        duration: 2000
      });
  	}
  }
  cardcreate(code){
  	var url='/crm/papi/v1.9/member';
  	var _this=this;
  	var memberid=this.state.member[this.state.cindex].id
  	var pay=this.state.member[this.state.cindex].price
  	var dates={
		  "realName": "",
		  "code":code,
		  "payTypeId":125,
		  "sex": 0,
		  "mobile":this.state.mobile,
		  "idCard": "",
		  "shopId":Taro.getStorageSync('shopId'),
		  "address": "",
		  "beforeLevelId": "",
		  "level": {
		    "id":memberid
		  }
		}
  	CommonJs.ajaxRequest(url,dates,'POST',function(res){
	     if(pay>0){
	     		res=res.data;
          Taro.requestPayment({
          	appId:res.appId,
            timeStamp: res.timeStamp,
            nonceStr: res.nonceStr,
            package: res.packageValue,
            signType: res.signType,
            paySign: res.paySign,
            success(res) {
              Taro.showToast({
								title:'开通成功',
								icon: 'none',
								duration: 2000
							});
				      setTimeout(function(){
				      	 CommonJs.open('/pages/card/cardlist')
				      },1000)
            },
            fail() {
              Taro.showToast({
                title:'支付失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
	     }
	     else{
	     	Taro.showToast({
					title:'开通成功',
					icon: 'none',
					duration: 2000
				});
	      setTimeout(function(){
	      	 CommonJs.open('/pages/card/cardlist')
	      },1000)
	     }
	  },'wfw')
  }
  getphone(){
  	var url='/user/getUserInfo'
		var _this=this;
		CommonJs.ajaxRequest(url,'','GET',function(ret){
			if(ret.content){
	  			_this.setState({
		        mobile:ret.content.mobile
		      })
	  		}
		})
  }
  render () {
    const member = this.state.member
    const quninfo=this.state.quninfo
    const shopinfo=this.state.shopinfo
    const cindex=this.state.cindex;
    return (
      <View className='page' style={this.state.isload==1?'display:block':'display:none'}>
      	<View className='card-page'>
      			<View className='card-input'>
      					<View className='card-title'>手机号码</View>
      					<Input className='cardinput' value={this.state.mobile} id='mobile' type='text' placeholder='线下可出示手机号享受折扣' focus onInput={this.getInputVal.bind(this)}/>
      			</View>
      			{member&&member.map((item, index) => (
	      			<View className={index==cindex?'card-li card-licur':'card-li'}  onClick={this.choose.bind(this,index)}>
	      					<View className='card-h3'>
	      						<Text className='card-name'>{item.name}</Text>
	      						<Text className='card-price'>{item.price/100}元</Text>
	      					</View>
	      					<View className='card-span'>本群消费享受{item.discount/100}折优惠,特价除外</View>
	      					<View className='card-qx' style={item.membershipInterests&&item.membershipInterests.length>0?'':'display:none'}>
	      						{item.membershipInterests&&item.membershipInterests.map((item1, index1) => (
	      						<View>
	      						<Text className='card-tit'>{item1.equityType.name}</Text>
	      						{item1.effectiveConditions&&item1.effectiveConditions.map((item2, index2) => (
	      							<View className='card-goods' style={item2.conditionName?'':'display:none'}>
	      								<View className='card-dl'>
				      						<View className='card-dt'>
				      							<Image className="cat-img" mode="widthFix" src={item2.logo}></Image>
				      						</View>
				      						<View  className='card-dd'>
				      							<Text className='card-h3'>{item2.conditionName}*{item2.number}</Text>
				      						</View>
				      					</View>
	      								
	      							</View>
	      						))}
	      						</View>
	      						))}
	      					</View>
	      			</View>
      			))}
      			
      			<View className='card-tips' style={member.length>0?"":"display:none"}>适用于{quninfo.name}社群内的所有活动,商城的消费折扣,以及{shopinfo.name}线下门店内所有的消费</View>
      			<View className='card-tips' style={member.length>0?"display:none":""}>该商家暂无开通会员等级功能,敬请期待</View>
      			<View className='card-btn' style={member.length>0?"":"display:none"}  onClick={this.ktfun.bind(this)}>开通会员</View>
      			
      	</View>
      </View>
    )
  }
}
